package com.network.action;

import java.util.Enumeration;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;

import com.network.cfg.Configuration;
import com.network.entity.User;
import com.network.service.IUserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author Ling Xiaowei
 * 
 */
@Component
public class MainAction extends ActionSupport {

	private String username;

	private String password;

	private IUserService userService;
	
	public String loginOut(){
		cleanSession();
		return SUCCESS;
	}

	public String gotoLogin() {
		return SUCCESS;
	}

	public String login() {
		ActionContext ctx = ActionContext.getContext();
		User user = userService.getUserById(username);
		if (user == null || !user.getPassword().equals(password)) {
			ctx.put("error", "用户名不存在或者密码错误");
			return Configuration.GOTO_LOGIN;
		} else {
			// Do nothing
		}
		password = null;
		ctx.getSession().put(Configuration.USER, user);
		if (user.getRole().equals(Configuration.ADMIN_FLAG)) {
			return Configuration.GOTO_ADMIN_HOMEPAGE;
		} else {
			return Configuration.GOTO_USER_HOMEPAGE;

		}

	}

	public void cleanSession() {
		ActionContext.getContext().getSession().clear();
		
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public IUserService getUserService() {
		return userService;
	}

	@Resource(name = "userServiceImpl")
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

}
